﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Zero.DataLayer;
using Zero.POCO;
using Zero.POCO.Compagne;
using Zero.POCO.Donnees;
using Zero.POCO.Enum;
using Zero.POCO.Enum.Helper.Support;
using Zero.POCO.Helper.Support;
using Zero.POCO.Support;
using Zero.Presenter.Scheduler;

namespace DevExpress.MailClient.Win.Controls
{
    public partial class UcPlaning : DevExpress.XtraEditors.XtraUserControl, ISchedulerView
    {

       


        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public event EventHandler<EventArgsMedia> ChangeMediaEvent;


        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public List<SupportPresse> SupportPresses { get; set; }
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public List<SupportTV> SupportTvs { get; set; }
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public List<SupportRadio> SupportRadios { get; set; }
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public List<Filter> Filters { get; set; }
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public List<Vague> Vagues { get; set; }

        public List<PlanTV> PlanTvs { get; set; }
        public List<PlanRadio> PlanRadios { get; set; }
        public List<PlanPresse> PlanPresses { get; set; }

        public UcPlaning()
        {
            InitializeComponent();
            var presenterScheduler = new SchedulerPresenter(this, new SupportPresseRepository(), new SupportTvRepository(),
              new SupportRadioRepository(), new FilterRepository(),new VagueRepository(),new PlanTvRepository(), new PlanRdRepository(), new PlanPrRepository());

            presenterScheduler.Display();


            cbVague.DataSource = Vagues;
            cbVague.DisplayMember = "libelle";

            cbFiltre.DataSource = Filters;
            cbFiltre.DisplayMember = "Nom";
            cbPlanTv.DataSource = PlanTvs;
            cbPlanTv.DisplayMember = "Name";
            cbPlanTv.SelectedIndex = -1;

            cbPlanRD.DataSource = PlanRadios;
            cbPlanRD.DisplayMember = "Name";
            cbPlanPR.DataSource = PlanPresses;
            cbPlanPR.DisplayMember = "Name";
            cbPlanPR.SelectedIndex = -1;


            cbMedia.Items.Add("Television");
            cbMedia.Items.Add("Radio");
            cbMedia.Items.Add("Presse");
                
            cbMedia.SelectedIndex = 0;
            cbSupport.DataSource = SupportTvs;
            cbSupport.DisplayMember = "Libelle";
        }
        /// <summary>
        /// juste pour appeler la methode privé de l'evenement et contourner le fait qu'elle soit en privé
        /// </summary>
        public void placebo()
        {
            cbSupport_SelectedIndexChanged(null, null);
            
        }
        private void cbSupport_SelectedIndexChanged(object sender, EventArgs e)
        {
           SupportRadio valuesupportRd = null;
            SupportTV valuesupportTv = null;
            SupportPresse ValuesupportPr = null;


            string valueSupport = null;
            var valueMedia = cbMedia.SelectedItem.ToString();
            switch (cbMedia.SelectedIndex)
            {
                case 0:{
                        var listeDonne = new List<string>();
                        for (int i = 1; i < 20; i++)
                        {
                            listeDonne.Add(5 * i + " secondes");
                        }
                        cbFormat.DataSource = listeDonne;
                        valueSupport = ((SupportTV)cbSupport.SelectedValue).Libelle;
                        valuesupportTv = (SupportTV) cbSupport.SelectedValue;

                        break;
                    }

                case 1:
                    {
                        var listeDonne = new List<string>();
                        for (int i = 1; i < 20; i++)
                        {
                            listeDonne.Add(5 * i + " secondes");
                        }
                        cbFormat.DataSource = listeDonne;
                        valueSupport = ((SupportRadio)cbSupport.SelectedValue).Libelle;
                        valuesupportRd = (SupportRadio) cbSupport.SelectedValue;
                        break;
                    }

                case 2:
                    {
                        cbFormat.DataSource = Enum.GetValues(typeof(FormatPresse));

                        valueSupport = ((SupportPresse)cbSupport.SelectedValue).Libelle;
                        ValuesupportPr = (SupportPresse) cbSupport.SelectedValue;
                        break;
                    }
            }

            var valueFormat = cbFormat.SelectedIndex.ToString();var valueListesLibelle = new List<String>();valueListesLibelle.AddRange(SupportTvs.Select(es => es.Libelle));
            valueListesLibelle.AddRange(SupportRadios.Select(es => es.Libelle));
            valueListesLibelle.AddRange(SupportPresses.Select(es => es.Libelle));
            var valueVague = (Vague)cbVague.SelectedItem;
            var valueFiltre = (Filter)cbFiltre.SelectedItem;
            var valuePlanTv = (PlanTV)cbPlanTv.SelectedItem;
            var valuePlanRd = (PlanRadio)cbPlanRD.SelectedItem;
            var valuePlanPr = (PlanPresse)cbPlanPR.SelectedItem;

            var argument = new EventArgsMedia()
            {
                PlanPresse = valuePlanPr,
                PlanRadio = valuePlanRd,
                PlanTV = valuePlanTv,
                Support = valueSupport,
                Media = valueMedia,
                Format = valueFormat,
                Libelle = valueListesLibelle,

                filtre = valueFiltre,
                vague = valueVague,

                SupportPr = ValuesupportPr,
                SupportTv = valuesupportTv,
                SupportRd = valuesupportRd
            };
            EventHandler<EventArgsMedia> mediaEvent = ChangeMediaEvent;
            if (mediaEvent != null)
                mediaEvent(this, argument);
        }

        private void cbMedia_SelectedIndexChanged(object sender, EventArgs e)
        {

            SupportRadio valuesupportRd = null;
            SupportTV valuesupportTv = null;
            SupportPresse ValuesupportPr = null;
            string valueSupport=null;

                switch (cbMedia.SelectedIndex)
                {
                    case 0:
                    {
                        cbSupport.DataSource = SupportTvs;
                        var listeDonne=new List<string>();
                        for (int i = 1; i < 20; i++)
                        {
                            listeDonne.Add(5 * i + " secondes");
                        }
                        cbFormat.DataSource = listeDonne;
                        valueSupport = ((SupportTV)cbSupport.SelectedValue).Libelle;

                        valuesupportTv = (SupportTV)cbSupport.SelectedValue;
                        break;
                    }

                    case 1:
                    {
                        cbSupport.DataSource = SupportRadios;
                        var listeDonne = new List<string>();
                        for (int i = 1; i < 20; i++)
                        {
                            listeDonne.Add(5 * i + " secondes");
                        }
                        cbFormat.DataSource = listeDonne;
                        valueSupport = ((SupportRadio)cbSupport.SelectedValue).Libelle;

                        valuesupportRd = (SupportRadio)cbSupport.SelectedValue;
                        break;
                    }

                    case 2:
                    {
                        cbSupport.DataSource = SupportPresses;
                        cbFormat.DataSource = Enum.GetValues(typeof(FormatPresse));

                        valueSupport = ((SupportPresse)cbSupport.SelectedValue).Libelle;

                        ValuesupportPr = (SupportPresse)cbSupport.SelectedValue;
                        break;
                    }    
                }

                var valueMedia = cbMedia.SelectedItem.ToString();
                var valueFormat = cbFormat.SelectedIndex.ToString();

            var valueListesLibelle = new List<String>();
            valueListesLibelle.AddRange(SupportTvs.Select(es=>es.Libelle));
            valueListesLibelle.AddRange(SupportRadios.Select(es => es.Libelle));
            valueListesLibelle.AddRange(SupportPresses.Select(es => es.Libelle));
            var valueVague = (Vague)cbVague.SelectedItem;
            var valueFiltre = (Filter)cbFiltre.SelectedItem;
            var valuePlanTv = (PlanTV)cbPlanTv.SelectedItem;
            var valuePlanRd = (PlanRadio)cbPlanRD.SelectedItem;
            var valuePlanPr = (PlanPresse)cbPlanPR.SelectedItem;

            var argument = new EventArgsMedia()
            {
                PlanPresse = valuePlanPr,
                PlanRadio = valuePlanRd,
                PlanTV = valuePlanTv,
                    Support = valueSupport,
                    Media = valueMedia,
                    Format = valueFormat,
                    Libelle = valueListesLibelle,

                    filtre = valueFiltre,
                    vague = valueVague,

                    SupportPr = ValuesupportPr,
                    SupportTv = valuesupportTv,
                    SupportRd = valuesupportRd
                };
                var mediaEvent = ChangeMediaEvent;
            if (mediaEvent != null)
                mediaEvent(this, argument);
        }
        private void cbVague_SelectedIndexChanged(object sender, EventArgs e)
        {
            SupportRadio valuesupportRd = null;
            SupportTV valuesupportTv = null;
            SupportPresse ValuesupportPr = null;


            string valueSupport = null;
            switch (cbMedia.SelectedIndex)
            {
                case 0:
                    {
                      
                        valueSupport = ((SupportTV)cbSupport.SelectedValue).Libelle;

                        valuesupportTv = (SupportTV)cbSupport.SelectedValue;
                        break;
                    }

                case 1:
                    {

                        valueSupport = ((SupportRadio)cbSupport.SelectedValue).Libelle;
                        valuesupportRd = (SupportRadio)cbSupport.SelectedValue;
                        break;
                    }

                case 2:
                    {

                        valueSupport = ((SupportPresse)cbSupport.SelectedValue).Libelle;
                        ValuesupportPr = (SupportPresse)cbSupport.SelectedValue;
                        break;
                    }
            }
            string valueMedia;
            valueMedia = cbMedia.SelectedItem != null ? cbMedia.SelectedItem.ToString() : null;

            string valueFormat;
            valueFormat =cbFormat.SelectedIndex!=null? cbFormat.SelectedIndex.ToString(): null;

            var valueListesLibelle = new List<String>();
            valueListesLibelle.AddRange(SupportTvs.Select(es => es.Libelle));
            valueListesLibelle.AddRange(SupportRadios.Select(es => es.Libelle));
            valueListesLibelle.AddRange(SupportPresses.Select(es => es.Libelle));


            var valueVague=(Vague)cbVague.SelectedItem;
            var valueFiltre = (Filter)cbFiltre.SelectedItem;

            var valuePlanTv = (PlanTV)cbPlanTv.SelectedItem;
            var valuePlanRd = (PlanRadio)cbPlanRD.SelectedItem;
            var valuePlanPr = (PlanPresse)cbPlanPR.SelectedItem;

            var argument = new EventArgsMedia()
            {
                PlanPresse = valuePlanPr,
                PlanRadio = valuePlanRd,
                PlanTV = valuePlanTv,
                Support = valueSupport,
                Media = valueMedia,
                Format = valueFormat,
                Libelle = valueListesLibelle,
                filtre = valueFiltre,
                vague = valueVague,

                SupportPr = ValuesupportPr,
                SupportTv = valuesupportTv,
                SupportRd = valuesupportRd
            };
            var mediaEvent = ChangeMediaEvent;
            if (mediaEvent != null)
                mediaEvent(this, argument);
        }

        private void cbFiltre_SelectedIndexChanged(object sender, EventArgs e)
        {
            SupportRadio valuesupportRd = null;
            SupportTV valuesupportTv = null;
            SupportPresse ValuesupportPr = null;
            string valueSupport = null;
            switch (cbMedia.SelectedIndex)
            {
                case 0:
                    {

                        valueSupport = ((SupportTV)cbSupport.SelectedValue).Libelle;

                        valuesupportTv = (SupportTV)cbSupport.SelectedValue;
                        break;
                    }

                case 1:
                    {

                        valueSupport = ((SupportRadio)cbSupport.SelectedValue).Libelle;
                        valuesupportRd = (SupportRadio)cbSupport.SelectedValue;
                        break;
                    }

                case 2:
                    {

                        valueSupport = ((SupportPresse)cbSupport.SelectedValue).Libelle;
                        ValuesupportPr = (SupportPresse)cbSupport.SelectedValue;
                        break;
                    }
            }
            string valueMedia;
            valueMedia = cbMedia.SelectedItem != null ? cbMedia.SelectedItem.ToString() : null;

            string valueFormat;
            valueFormat = cbFormat.SelectedIndex != null ? cbFormat.SelectedIndex.ToString() : null;

            var valueListesLibelle = new List<String>();
            valueListesLibelle.AddRange(SupportTvs.Select(es => es.Libelle));
            valueListesLibelle.AddRange(SupportRadios.Select(es => es.Libelle));
            valueListesLibelle.AddRange(SupportPresses.Select(es => es.Libelle));


            var valueVague = (Vague)cbVague.SelectedItem;
            var valueFiltre = (Filter)cbFiltre.SelectedItem;
            var valuePlanTv = (PlanTV)cbPlanTv.SelectedItem;
            var valuePlanRd = (PlanRadio)cbPlanRD.SelectedItem;
            var valuePlanPr = (PlanPresse)cbPlanPR.SelectedItem;

            var argument = new EventArgsMedia()
            {
                PlanPresse = valuePlanPr,
                PlanRadio = valuePlanRd,
                PlanTV = valuePlanTv,
                Support = valueSupport,
                Media = valueMedia,
                Format = valueFormat,
                Libelle = valueListesLibelle,
                filtre = valueFiltre,
                vague = valueVague,

                SupportPr = ValuesupportPr,
                SupportTv = valuesupportTv,
                SupportRd = valuesupportRd
            };
            var mediaEvent = ChangeMediaEvent;
            if (mediaEvent != null)
                mediaEvent(this, argument);
        }

        private void cbFormat_SelectedIndexChanged(object sender, EventArgs e)
        {
            SupportRadio valuesupportRd = null;
            SupportTV valuesupportTv = null;
            SupportPresse ValuesupportPr = null;
            string valueSupport = null;
            switch (cbMedia.SelectedIndex)
            {
                case 0:{

                        valueSupport = ((SupportTV)cbSupport.SelectedValue).Libelle;

                        valuesupportTv = (SupportTV)cbSupport.SelectedValue;
                        break;
                    }

                case 1:
                    {

                        valueSupport = ((SupportRadio)cbSupport.SelectedValue).Libelle;
                        valuesupportRd = (SupportRadio)cbSupport.SelectedValue;
                        break;
                    }

                case 2:
                    {

                        valueSupport = ((SupportPresse)cbSupport.SelectedValue).Libelle;
                        ValuesupportPr = (SupportPresse)cbSupport.SelectedValue;
                        break;
                    }
            }
            string valueMedia;
            valueMedia = cbMedia.SelectedItem != null ? cbMedia.SelectedItem.ToString() : null;

            string valueFormat;
            valueFormat = cbFormat.SelectedIndex != null ? cbFormat.SelectedIndex.ToString() : null;

            var valueListesLibelle = new List<String>();
            valueListesLibelle.AddRange(SupportTvs.Select(es => es.Libelle));
            valueListesLibelle.AddRange(SupportRadios.Select(es => es.Libelle));
            valueListesLibelle.AddRange(SupportPresses.Select(es => es.Libelle));


            var valueVague = (Vague)cbVague.SelectedItem;
            var valueFiltre = (Filter)cbFiltre.SelectedItem;
            var valuePlanTv = (PlanTV)cbPlanTv.SelectedItem;
            var valuePlanRd = (PlanRadio)cbPlanRD.SelectedItem;
            var valuePlanPr = (PlanPresse)cbPlanPR.SelectedItem;

            var argument = new EventArgsMedia()
            {
                PlanPresse = valuePlanPr,
                PlanRadio = valuePlanRd,
                PlanTV = valuePlanTv,
                Support = valueSupport,
                Media = valueMedia,
                Format = valueFormat,
                Libelle = valueListesLibelle,
                filtre = valueFiltre,
                vague = valueVague,

                SupportPr = ValuesupportPr,
                SupportTv = valuesupportTv,
                SupportRd = valuesupportRd
            };
            var mediaEvent = ChangeMediaEvent;
            if (mediaEvent != null)
                mediaEvent(this, argument);
        }

    

        private void cbPlanTv_SelectedIndexChanged(object sender, EventArgs e)
        {
            SupportRadio valuesupportRd = null;
            SupportTV valuesupportTv = null;
            SupportPresse ValuesupportPr = null;
            string valueSupport = null;
            switch (cbMedia.SelectedIndex)
            {
                case 0:
                    {

                        valueSupport = ((SupportTV)cbSupport.SelectedValue).Libelle;

                        valuesupportTv = (SupportTV)cbSupport.SelectedValue;
                        break;
                    }

                case 1:
                    {

                        valueSupport = ((SupportRadio)cbSupport.SelectedValue).Libelle;
                        valuesupportRd = (SupportRadio)cbSupport.SelectedValue;
                        break;
                    }

                case 2:
                    {

                        valueSupport = ((SupportPresse)cbSupport.SelectedValue).Libelle;
                        ValuesupportPr = (SupportPresse)cbSupport.SelectedValue;
                        break;
                    }
            }
            string valueMedia;
            valueMedia = cbMedia.SelectedItem != null ? cbMedia.SelectedItem.ToString() : null;

            string valueFormat;
            valueFormat = cbFormat.SelectedIndex != null ? cbFormat.SelectedIndex.ToString() : null;

            var valueListesLibelle = new List<String>();
            valueListesLibelle.AddRange(SupportTvs.Select(es => es.Libelle));
            valueListesLibelle.AddRange(SupportRadios.Select(es => es.Libelle));
            valueListesLibelle.AddRange(SupportPresses.Select(es => es.Libelle));


            var valueVague = (Vague)cbVague.SelectedItem;
            var valueFiltre = (Filter)cbFiltre.SelectedItem;


            var valuePlanTv = (PlanTV) cbPlanTv.SelectedItem;
            var valuePlanRd = (PlanRadio)cbPlanRD.SelectedItem;
            var valuePlanPr = (PlanPresse)cbPlanPR.SelectedItem;

            var argument = new EventArgsMedia()
            {
                PlanPresse = valuePlanPr,
                PlanRadio = valuePlanRd,
                PlanTV = valuePlanTv,

                Support = valueSupport,
                Media = valueMedia,
                Format = valueFormat,
                Libelle = valueListesLibelle,
                filtre = valueFiltre,
                vague = valueVague,

                SupportPr = ValuesupportPr,
                SupportTv = valuesupportTv,
                SupportRd = valuesupportRd
            };
            var mediaEvent = ChangeMediaEvent;
            if (mediaEvent != null)
                mediaEvent(this, argument);
        }

        private void cbPlanRD_SelectedIndexChanged(object sender, EventArgs e)
        {
            SupportRadio valuesupportRd = null;
            SupportTV valuesupportTv = null;
            SupportPresse ValuesupportPr = null;
            string valueSupport = null;
            switch (cbMedia.SelectedIndex)
            {
                case 0:
                    {

                        valueSupport = ((SupportTV)cbSupport.SelectedValue).Libelle;

                        valuesupportTv = (SupportTV)cbSupport.SelectedValue;
                        break;
                    }

                case 1:
                    {

                        valueSupport = ((SupportRadio)cbSupport.SelectedValue).Libelle;
                        valuesupportRd = (SupportRadio)cbSupport.SelectedValue;
                        break;
                    }

                case 2:
                    {

                        valueSupport = ((SupportPresse)cbSupport.SelectedValue).Libelle;
                        ValuesupportPr = (SupportPresse)cbSupport.SelectedValue;
                        break;
                    }
            }
            string valueMedia;
            valueMedia = cbMedia.SelectedItem != null ? cbMedia.SelectedItem.ToString() : null;

            string valueFormat;
            valueFormat = cbFormat.SelectedIndex != null ? cbFormat.SelectedIndex.ToString() : null;

            var valueListesLibelle = new List<String>();
            valueListesLibelle.AddRange(SupportTvs.Select(es => es.Libelle));
            valueListesLibelle.AddRange(SupportRadios.Select(es => es.Libelle));
            valueListesLibelle.AddRange(SupportPresses.Select(es => es.Libelle));


            var valueVague = (Vague)cbVague.SelectedItem;
            var valueFiltre = (Filter)cbFiltre.SelectedItem;


            var valuePlanTv = (PlanTV)cbPlanTv.SelectedItem;
            var valuePlanRd = (PlanRadio)cbPlanRD.SelectedItem;
            var valuePlanPr = (PlanPresse)cbPlanPR.SelectedItem;

            var argument = new EventArgsMedia()
            {
                PlanPresse = valuePlanPr,
                PlanRadio = valuePlanRd,
                PlanTV = valuePlanTv,

                Support = valueSupport,
                Media = valueMedia,
                Format = valueFormat,
                Libelle = valueListesLibelle,
                filtre = valueFiltre,
                vague = valueVague,

                SupportPr = ValuesupportPr,
                SupportTv = valuesupportTv,
                SupportRd = valuesupportRd
            };
            var mediaEvent = ChangeMediaEvent;
            if (mediaEvent != null)
                mediaEvent(this, argument);
        }

        private void cbPlanPR_SelectedIndexChanged(object sender, EventArgs e)
        {
            SupportRadio valuesupportRd = null;
            SupportTV valuesupportTv = null;
            SupportPresse ValuesupportPr = null;
            string valueSupport = null;
            switch (cbMedia.SelectedIndex)
            {
                case 0:
                    {

                        valueSupport = ((SupportTV)cbSupport.SelectedValue).Libelle;

                        valuesupportTv = (SupportTV)cbSupport.SelectedValue;
                        break;
                    }

                case 1:
                    {

                        valueSupport = ((SupportRadio)cbSupport.SelectedValue).Libelle;
                        valuesupportRd = (SupportRadio)cbSupport.SelectedValue;
                        break;
                    }

                case 2:
                    {

                        valueSupport = ((SupportPresse)cbSupport.SelectedValue).Libelle;
                        ValuesupportPr = (SupportPresse)cbSupport.SelectedValue;
                        break;
                    }
            }
            string valueMedia;
            valueMedia = cbMedia.SelectedItem != null ? cbMedia.SelectedItem.ToString() : null;

            string valueFormat;
            valueFormat = cbFormat.SelectedIndex != null ? cbFormat.SelectedIndex.ToString() : null;

            var valueListesLibelle = new List<String>();
            valueListesLibelle.AddRange(SupportTvs.Select(es => es.Libelle));
            valueListesLibelle.AddRange(SupportRadios.Select(es => es.Libelle));
            valueListesLibelle.AddRange(SupportPresses.Select(es => es.Libelle));


            var valueVague = (Vague)cbVague.SelectedItem;
            var valueFiltre = (Filter)cbFiltre.SelectedItem;


            var valuePlanTv = (PlanTV)cbPlanTv.SelectedItem;
            var valuePlanRd = (PlanRadio)cbPlanRD.SelectedItem;
            var valuePlanPr = (PlanPresse)cbPlanPR.SelectedItem;

            var argument = new EventArgsMedia()
            {
                PlanPresse = valuePlanPr,
                PlanRadio = valuePlanRd,
                PlanTV = valuePlanTv,

                Support = valueSupport,
                Media = valueMedia,
                Format = valueFormat,
                Libelle = valueListesLibelle,
                filtre = valueFiltre,
                vague = valueVague,

                SupportPr = ValuesupportPr,
                SupportTv = valuesupportTv,
                SupportRd = valuesupportRd
            };
            var mediaEvent = ChangeMediaEvent;
            if (mediaEvent != null)
                mediaEvent(this, argument);
        }
    }

    public class EventArgsMedia : EventArgs
    {
        public string Support { get; set; }
        public string Media { get; set; }

        public string Format { get; set; }

        public List<String> Libelle { get; set; }

        public Vague vague { get; set; }

        public Filter filtre { get; set; }

        public SupportTV SupportTv { get; set; }
        public SupportRadio SupportRd { get; set; }
        public SupportPresse SupportPr { get; set; }

        public PlanTV PlanTV { get; set; }
        public PlanRadio PlanRadio { get; set; }
        public PlanPresse PlanPresse { get; set; }


    }
}